﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity;
using System.Data.EntityClient;
using System.Data.EntityModel;
using System.Data.Objects.DataClasses;
using System.Reflection;
using System.Data;
using FCModel;

namespace FCDAL
{
    public class OcrDAL
    {
        public DataTable GetOcrData(string SiteCode, string HSASN)
        {
            DbContext db = EFContextFactory.GetOcrDbContext(SiteCode);
            return db.Database.SqlQuery<OCR_HGSAResonance>("SELECT * FROM ufn_OCR_HGSAResonance('" + HSASN + "') order by HSA_SERIAL_NUM").ToList().ToDataTable();  //
        }
    }

    static class Extensions
    {
        internal static DataTable ToDataTable<T>(this IList<T> list)
        {
            Type elementType = typeof(T);
            var t = new DataTable();
            elementType.GetProperties().ToList().ForEach(propInfo => t.Columns.Add(propInfo.Name, Nullable.GetUnderlyingType(propInfo.PropertyType) ?? propInfo.PropertyType));
            foreach (T item in list)
            {
                var row = t.NewRow();
                elementType.GetProperties().ToList().ForEach(propInfo => row[propInfo.Name] = propInfo.GetValue(item, null) ?? DBNull.Value);
                t.Rows.Add(row);
            }
            return t;
        }
    }
}
